package com.seveneight.baijiesteward.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.PlanDetailListBean;
import com.seveneight.baijiesteward.bean.RankListBean;
import com.seveneight.baijiesteward.http.HttpConfig;
import com.seveneight.baijiesteward.http.JsonFormat;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.main.activity.BankPlanInfoActivity;
import com.seveneight.baijiesteward.ui.main.activity.RePayPlanInfoActivity;
import com.seveneight.baijiesteward.ui.user.model.PlanRePayRemoveModel;
import com.seveneight.baijiesteward.utils.DateUtil;
import com.seveneight.baijiesteward.utils.MyListView;
import com.seveneight.baijiesteward.views.cover.SwipeMenuLayout;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 计划列表adapter
 * Created by user1 on 2018/5/31.
 */

public class PlanListAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    List<RankListBean> listData;
    PlanRePayRemoveModel apiRemove=new PlanRePayRemoveModel();
    Activity activity;
    public PlanListAdapter(Activity activity, List<RankListBean> listData){
        mInflater = LayoutInflater.from(activity);
        this.activity=activity;
        this.listData=listData;
    }

    @Override
    public int getCount() {
        return null==listData?0:listData.size();
    }
    public void refresh(List<RankListBean> listData){
        this.listData=listData;
        this.notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return null==listData?0:listData.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_plan_pay_list_layout, null); //加载布局
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
         }
        RankListBean bean=listData.get(i);
        holder.item_plan_date.setText(""+DateUtil.formatDate(bean.getCreatorTime()));
        holder.plan_number.setText(""+bean.getPlanNo());
        holder.repay_amount1.setText("￥"+bean.getRechargeAmount());
        String outBankName=bean.getOutCardNo();
        holder.out_bank_name.setText(bean.getOutBankName()+"("+outBankName.substring(outBankName.length()- 4)+")");
        String inBankName=bean.getInCardNo();
        holder.in_bank_name.setText(bean.getInBankName()+"("+inBankName.substring(inBankName.length()- 4)+")");
        Glide.with(activity).load(HttpConfig.SOURCE_URL+bean.getOutBankIcon()).into(holder.plan_photo1);
        Glide.with(activity).load(HttpConfig.SOURCE_URL+bean.getInBankIcon()).into(holder.plan_photo2);


        holder.plan_no.setText("订单号 :   "+bean.getPlanNo());
        holder.brokerage.setText("还款手续费 ￥"+(bean.getRepayCount()*bean.getBrokerage())+"");
        DecimalFormat df=new DecimalFormat("######0.00");
        holder.service_charge.setText("消费手续费 ￥"+df.format((bean.getServiceCharge()-(bean.getRepayCount()*bean.getBrokerage())))+"");
         holder.repay_amount.setText("还款金额 ￥"+bean.getRechargeAmount());
         if (bean.getPlanStatus()==4){
             holder.status_photo.setImageResource(R.mipmap.success);
             holder.status_name.setText("执行成功");
             holder.status_name.setTextColor(Color.parseColor("#58de74"));
         }else if(bean.getPlanStatus()==3){
             holder.status_photo.setImageResource(R.mipmap.performed);
             holder.status_name.setText("执行中");
             holder.status_name.setTextColor(Color.parseColor("#53affe"));
         }else if(bean.getPlanStatus()==2){
             holder.status_photo.setImageResource(R.mipmap.error_list);
             holder.status_name.setText("执行失败");
             holder.status_name.setTextColor(Color.parseColor("#fd4519"));
         }else if(bean.getPlanStatus()==1){
             holder.status_photo.setImageResource(R.mipmap.waiting);
             holder.status_name.setText("等待执行");
             holder.status_name.setTextColor(Color.parseColor("#ffa400"));
         }
        holder.btnDelete.setVisibility(bean.getPlanStatus()==1?View.VISIBLE:View.GONE);
        ViewHolder finalHolder = holder;
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 remoreData(bean.getPlanId(),i);
                 ((SwipeMenuLayout) finalHolder.view).quickClose();
             }
         });
        holder.item_plan_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent=new Intent(activity,RePayPlanInfoActivity.class);
                Intent intent=new Intent(activity,BankPlanInfoActivity.class);
                intent.putExtra("data",bean);
//                intent.putExtra("id",bean.getPlanId());
//                intent.putExtra("type",bean.getPlanStatus());
                activity.startActivityIfNeeded(intent,18);
            }
        });
//         holder.item_pay_time.setText(bean.getExecuteTime());
//         if (null != bean.getList()){
//
//         }

        return convertView;
    }
    public void remoreData(String id,int index){
        new MaterialDialog.Builder(activity)
                .content("是否取消执行")
                .contentColorRes(R.color.graytext)
                .backgroundColorRes(R.color.white)
                .negativeColorRes(R.color.mainblue)
                .negativeText(R.string.negative)
                .positiveColorRes(R.color.mainblue)
                .positiveText(R.string.positive)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                        remove(id,index);
                    }
                })
                .build().show();
    }
    public void remove(String id,int index){
        Map<String,String> params=new HashMap<>();
        params.put("RepayPlanId",id);
        apiRemove.getBankList(activity, JsonFormat.getInstance(activity).AddCard(params), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                listData.remove(index);
                notifyDataSetChanged();
            }

            @Override
            public void onFailed() {

            }
        });

    }

    public class ViewHolder{
        TextView plan_no;
        TextView item_plan_date;
        TextView brokerage;
        TextView service_charge;
        TextView repay_amount1;
        TextView repay_amount;
        ImageView status_photo;
        TextView status_name;
        TextView plan_number;
        ImageView plan_photo1;
        ImageView plan_photo2;
        TextView out_bank_name;
        TextView in_bank_name;
        TextView btnDelete;
        LinearLayout item_plan_layout;
        View view;

        ViewHolder(View view){
            this.view=view;
            plan_no=view.findViewById(R.id.plan_no);
            item_plan_date=view.findViewById(R.id.item_plan_date);
            brokerage=view.findViewById(R.id.brokerage);
            service_charge=view.findViewById(R.id.service_charge);
            repay_amount1=view.findViewById(R.id.repay_amount1);
            repay_amount=view.findViewById(R.id.repay_amount);
            status_photo=view.findViewById(R.id.status_photo);
            status_name=view.findViewById(R.id.status_name);
            plan_number=view.findViewById(R.id.plan_number);
            plan_photo1=view.findViewById(R.id.plan_photo1);
            plan_photo2=view.findViewById(R.id.plan_photo2);
            out_bank_name=view.findViewById(R.id.out_bank_name);
            in_bank_name=view.findViewById(R.id.in_bank_name);
            item_plan_layout=view.findViewById(R.id.item_plan_layout);
            btnDelete=view.findViewById(R.id.btnDelete);
        }
    };
}
